home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / SCALE.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  9.1 KB  |  291 lines

  1. package sub_arctic.lib;
  2.  
  3. import sub_arctic.output.*;
  4. import sub_arctic.input.*;
  5.  
  6. /**
  7.  * This class implements a scale -- a horizontal scrollbar with no
  8.  * buttons on the ends and a fixed size thumb.  It uses the style
  9.  * system for its display.
  10.  * 
  11.  * @author Ian Smith
  12.  */
  13.  
  14. public class scale extends h_slider {
  15.  
  16.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  17.  
  18.   /**
  19.    * Full constructor for a scale.
  20.    * @param int xv       x position.
  21.    * @param int yv       y position.
  22.    * @param int wv       width in pixels.
  23.    * @param int minv     minimum slider value.
  24.    * @param int maxv     maximum slider value.
  25.    * @param int init_val initial slider value.
  26.    * @param int lincv    large increment value (the user clicks in the thumb 
  27.    *                     area but not on the thumb).
  28.    * @param callback_object obj the object which receives the callbacks. 
  29.    */
  30.   public scale(int xv, int yv, int wv, int minv, int maxv, int init_val,
  31.            int lincv,callback_object obj) {
  32.  
  33.     super(xv,yv,wv,minv,maxv,init_val,0 /* doesn't matter */,lincv,obj);
  34.  
  35.     /* normally, user's of the style system don't want boxed */
  36.     set_boxed(false);
  37.  
  38.     /* now build the images ... */
  39.     style_changed();
  40.   }
  41.  
  42.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  43.  
  44.   /**
  45.    * Constructor for a scale which assumes you will position it either
  46.    * directly or with constraints.
  47.    *
  48.    * @param int wv       width in pixels.
  49.    * @param int minv     minimum slider value.
  50.    * @param int maxv     maximum slider value.
  51.    * @param int init_val initial slider value.
  52.    * @param int lincv    large increment value (the user clicks in the thumb 
  53.    *                     area but not on the thumb).
  54.    * @param callback_object obj the object which receives the callbacks.
  55.    */
  56.   public scale(int wv, int minv, int maxv, int init_val,
  57.            int lincv,callback_object obj) {
  58.  
  59.     super(0,0,wv,minv,maxv,init_val,0 /* doesn't matter */,lincv,obj);
  60.  
  61.     /* normally, user's of the style system don't want boxed */
  62.     set_boxed(false);
  63.  
  64.     /* now build the images ... */
  65.     style_changed();
  66.   }
  67.  
  68.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  69.  
  70.   /**
  71.    * Constructor for a scale with some reasonable defaults. We default
  72.    * the initial value to the minimum value and the large increment
  73.    * to 1/4 of the range of the scale.  We assume you will use constraints
  74.    * or directly set the position of this object.
  75.    *
  76.    * @param int             wv   width in pixels
  77.    * @param int             minv minimum slider value
  78.    * @param int             maxv maximum slider value
  79.    * @param callback_object obj  the object which receives the callbacks.
  80.    */
  81.   public scale(int wv, int minv, int maxv,callback_object obj) {
  82.  
  83.     super(0,0,wv,minv,maxv,minv,0 /* doesn't matter */,(maxv-minv)/4,obj);
  84.  
  85.     /* normally, user's of the style system don't want boxed */
  86.     set_boxed(false);
  87.  
  88.     /* now build the images ... */
  89.     style_changed();
  90.   }
  91.  
  92.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  93.  
  94.   /**
  95.    * This function is called to create the appearance of the scale.
  96.    */
  97.   public void style_changed() {
  98.  
  99.     /* make the images */
  100.     style cs=style_manager.current_style();
  101.     loaded_image back,thumb;
  102.     back=cs.scale_background(w());
  103.     set_back_img(back);
  104.     thumb=cs.scale_thumb();
  105.     set_thumb_img(thumb);
  106.  
  107.     set_intrinsic_h(cs.scale_height());
  108.  
  109.     set_thumb_shift(cs.scale_thumb_shift());
  110.   }
  111.  
  112.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  113.  
  114.   /**
  115.    * Override the drawing code in h_slider_display to allow us to not
  116.    * have the end pieces of the scrollbar.
  117.    *
  118.    * @param drawable d the drawing surface to put the image on.
  119.    */
  120.   public void draw_self_local(drawable d) {
  121.     int off;
  122.     int shift=style_manager.current_style().scale_unusable_width();
  123.  
  124.     d.tileImage(_back_img,0,0,w(),h());
  125.  
  126.     /* compute offset for thumb */
  127.     off = thumb_offset();
  128.  
  129.     /* draw the thumb if we have room for one */
  130.     if (off >= 0) {
  131.       d.drawImage(_thumb_img, off + shift, thumb_shift());
  132.     }
  133.  
  134.     /* draw a box around the whole thing if that flag is set 
  135.      * (which it is by default) */
  136.     if (boxed()) {
  137.       d.setColor(style_manager.default_color_scheme().foreground()); 
  138.       d.drawRect(0,0, w()-1,h()-1);
  139.     }
  140.   }
  141.  
  142.    //had:
  143.    //* @exception general PROPAGATED
  144.  
  145.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  146.  
  147.   /**
  148.    * We override the function that computes the offset of the thumb
  149.    * to ignore the areas that normally would be used for the end pieces.
  150.    * 
  151.    * @return int the amount the thumb should be shifted right to account for 
  152.    *             the value of the scale.
  153.    */
  154.   protected int thumb_offset() {
  155.     int off, slide_range, value_range;
  156.  
  157.     /* compute ranges for thumb */
  158.     slide_range = w() - _thumb_img.width() - 
  159.       (2*style_manager.current_style().scale_unusable_width());
  160.     value_range = max_val() - min_val();
  161.  
  162.     /* return negative if there is no room for the thumb */
  163.     if (slide_range <= 0 || value_range <= 0) return -1;
  164.     
  165.     /* otherwise compute drawing offset for thumb */
  166.     off = (value() - min_val()) * slide_range / value_range;
  167.     if (off < 0) off = 0;
  168.     if (off > slide_range) off = slide_range;
  169.     return off;
  170.   }
  171.  
  172.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  173.  
  174.   /**
  175.    * Override the press behavior to avoid the system getting confused
  176.    * about us not having the end pieces.
  177.    *
  178.    * @param event  evt       the press event (mouse down).
  179.    * @param Object user_info the information that was handed to the 
  180.    *                         pick_collector when this object was deemed to be 
  181.    *                         picked (this value is given to the drag focus 
  182.    *                         agent also, so it will be propagated along to the 
  183.    *                         drag calls below).
  184.    * @return boolean true if we consumed this event, which we will.
  185.    */
  186.   public boolean press(event evt, Object user_info) {
  187.     int off;
  188.  
  189.     /* compute the offset to the thumb */
  190.     off = thumb_offset();
  191.  
  192.     /* if there is no thumb, bail out now */
  193.     if (off < 0) return false;
  194.  
  195.     /* before thumb? */
  196.     if (evt.local_x() < off) {
  197.       /* do a large negative increment */
  198.       set_value(value() - large_inc());
  199.  
  200.       /* do callback */
  201.       dynamic_callback(evt);
  202.       static_callback(evt);
  203.       return true;
  204.     }
  205.  
  206.     /* in the thumb? */
  207.     if (evt.local_x() < off+_thumb_img.width()) {
  208.       /* make us the drag focus to track the thumb */
  209.       manager.simple_drag_focus.set_focus_to(this,evt,new int_holder());
  210.       return true;
  211.     }
  212.  
  213.     /* must be below the thumb, do a large positive increment */
  214.     set_value(value() + large_inc());
  215.     
  216.     /* do callback */
  217.     dynamic_callback(evt);
  218.     static_callback(evt);
  219.     return true;
  220.   }
  221.  
  222.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  223.  
  224.   /** 
  225.    * Start of a drag for the thumb.  Override to avoid the calculation
  226.    * for the left image. 
  227.    *
  228.    * @param event evt the event that is starting the drag (usually a press)
  229.    * @param Object user_info the object passed to the simple drag_agent 
  230.    */
  231.   public boolean drag_start(event evt, Object user_info) {
  232.     int off;
  233.  
  234.     /* remember how far away from top of thumb the event is */
  235.     off = thumb_offset();
  236.     ((int_holder)user_info).value = evt.local_x() - off;
  237.     return true;
  238.   }
  239.  
  240.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  241.  
  242.   /** 
  243.    * Movement within a drag of the thumb.  Overridden to avoid the
  244.    * calculations involving the end pieces.
  245.    *
  246.    * @param event  evt       the drag event (mouse move).
  247.    * @param Object user_info the object passed to the simple drag_agent.
  248.    */
  249.   public boolean drag_feedback(event evt, Object user_info) {
  250.     int off, slide_range;
  251.     int value_range, old_value;
  252.     
  253.     /* hold on to old value for a second */
  254.     old_value = value();
  255.       
  256.     /* determine new top of thumb */
  257.     off = evt.local_x() - ((int_holder)user_info).value;
  258.       
  259.     /* determine new value from this */
  260.     slide_range = w() - _thumb_img.width() - 
  261.       (2*style_manager.current_style().scale_unusable_width());
  262.  
  263.     value_range = max_val() - min_val();
  264.     set_value(min_val() + (off * value_range)/slide_range);
  265.       
  266.       /* if this is a new value, cause a redraw of us and do the callback */
  267.     if (value() != old_value) {
  268.       dynamic_callback(evt);
  269.     }
  270.     return true;
  271.   }
  272.  
  273.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  274. }
  275. /*=========================== COPYRIGHT NOTICE ===========================
  276.  
  277. This file is part of the subArctic user interface toolkit.
  278.  
  279. Copyright (c) 1996 Scott Hudson and Ian Smith
  280. All rights reserved.
  281.  
  282. The subArctic system is freely available for most uses under the terms
  283. and conditions described in 
  284.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  285. and appearing in full in the lib/interactor.java source file.
  286.  
  287. The current release and additional information about this software can be 
  288. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  289.  
  290. ========================================================================*/
  291.